package ro.imc.ums.dani.database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ro.imc.ums.dani.classes.Courses;



public class CourseCrud implements Crud {
	private Connection conn = null;

	public CourseCrud() {

	}

	@Override
	public void insert(Object o) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();

			Courses course = (Courses) o;

			String insert = "insert into courses"
					+ " (CourseName, CourseMandatory, CourseId, CourseSemester)"
					+ " values " + "('" + course.getCourseName() + "','"
					+ course.isMandatory() + "','" + course.getId() + "','"
					+ course.getSemester() + " ') ";

			sqlState.executeUpdate(insert);
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	@Override
	public ArrayList<Object> select() {
		ArrayList<Object> courseList = new ArrayList<Object>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();
			String select = "select * from courses";
			ResultSet result = sqlState.executeQuery(select);
			while (result.next()) {
				Courses course = new Courses(result.getString(2), Boolean.parseBoolean(result.getString(3)), 
											 Integer.parseInt(result.getString(4)), Integer.parseInt(result.getString(5)));
				courseList.add(course);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return courseList;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

}

